// 登录按钮点击事件处理
document.getElementById('loginBtn').addEventListener('click', handleLogin);

// 登录处理函数
async function handleLogin() {
    const button = document.getElementById('loginBtn');
    const status = document.getElementById('status');
    
    // 禁用按钮并显示加载状态
    button.disabled = true;
    button.innerHTML = '<i class="fas fa-spinner fa-spin mr-2"></i>登录中...';
    status.textContent = '正在处理登录请求...';
    
    try {
        // 模拟登录API调用
        const response = await fetch('/api/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                username: 'demo_user',
                password: 'demo_password',
                timestamp: new Date().toISOString()
            })
        });
        
        // 模拟网络延迟
        await new Promise(resolve => setTimeout(resolve, 1500));
        
        // 处理响应
        if (response.ok) {
            const data = await response.json();
            status.textContent = '登录成功！正在跳转...';
            status.className = 'mt-6 text-green-300 text-lg';
            
            // 模拟跳转
            setTimeout(() => {
                window.location.href = '/dashboard.html';
            }, 1000);
        } else {
            throw new Error('登录失败');
        }
        
    } catch (error) {
        // 处理错误
        console.error('登录错误:', error);
        status.textContent = '登录失败，请重试';
        status.className = 'mt-6 text-red-300 text-lg';
        
        // 重置按钮状态
        setTimeout(() => {
            button.disabled = false;
            button.innerHTML = '<i class="fas fa-sign-in-alt mr-2"></i>登录';
            status.textContent = '';
        }, 2000);
    }
}

// 添加键盘事件支持
document.addEventListener('keydown', (event) => {
    if (event.key === 'Enter') {
        handleLogin();
    }
});

// 页面加载完成后的初始化
document.addEventListener('DOMContentLoaded', () => {
    console.log('登录按钮示例已加载完成');
});
